from typing import List
from sqlalchemy.orm import Session
from db import models
from sqlalchemy import and_
from schemas.shopping_cart import CreateShoppingCartProductRequest, UpdateShoppingCartProductQuantityRequest


class ShoppingCart:
    @staticmethod
    def get_by_customer(db: Session, customer_id: int):
        return db.query(models.ShoppingCart).filter_by(customer_id=customer_id, status=1).all()

    @staticmethod
    def create(db: Session, data: CreateShoppingCartProductRequest) -> models.ShoppingCart:
        item = models.ShoppingCart(
            created_on=data.created_on,
            customer_id=data.customer_id,
            flower_id=data.flower_id,
            quantity=data.quantity
        )
        db.add(item)
        db.commit()
        return item

    @staticmethod
    def update_quantity(db: Session, data: UpdateShoppingCartProductQuantityRequest):
        db.query(models.ShoppingCart).filter_by(id=data.id).update({
            "updated_on": data.updated_on,
            "quantity": data.quantity
        })
        db.commit()

    @staticmethod
    def delete_many(db: Session, customer_id: int, ids: List[int]):
        db.query(models.ShoppingCart).filter(and_(
            models.ShoppingCart.customer_id == customer_id,
            models.ShoppingCart.id.in_(ids)
        )).delete()
        db.commit()
